package com.dog.basics.common.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import java.util.Random;

/**
 *  * 订单编码码生成器，生成32位数字编码，
 *  * @生成规则 1位单号类型+随机唯一戳+用户ID后四位
 *  * Date:2021年1月12日
 *  * @author he
 *  
 */
public class OrderCodeFactory {
    /**
     * 订单类别头
     */
    private static final String ORDER_CODE = "1";
    /**
     * 退货类别头
     */
    private static final String RETURN_ORDER = "2";
    /**
     * 退款类别头
     */
    private static final String REFUND_ORDER = "3";
    /**
     * 未付款重新支付别头
     */
    private static final String AGAIN_ORDER = "4";

    /**
     * 流水类别头
     */
    private static final String WATER_CODE = "5";

    /**
     * 二维码类别头
     */
    private static final String QR_CODE = "6";
    /**
     * 随即编码
     */
    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};
    /**
     * 用户id和随机数总长度
     */
    private static final int maxLength = 14;



    /**
     * 截取用户后四位
     * @param uid
     * @return
     */
    public static String getLongSoor(Long uid){
        // 判断是否为空
        String uidStr;
        if (ObjectUtil.isNull(uid)){
            uidStr = IdWorker.getIdStr();
        }else{
            uidStr = uid.toString();
            if (uidStr.length()<4){
                uidStr = IdWorker.getIdStr();
            }
        }
        // 获取后四位
        String sub = StrUtil.sub(uidStr, uidStr.length() - 4, uidStr.length());
        return sub;
    }



    /**
     *      * 更具id进行加密+加随机数组成固定长度编码
     *      
     */
    private static String toCode(Long id) {
        String idStr = id.toString();
        StringBuilder idsbs = new StringBuilder();
        for (int i = idStr.length() - 1; i >= 0; i--) {
            idsbs.append(r[idStr.charAt(i) - '0']);
        }
        return idsbs.append(getRandom(maxLength - idStr.length())).toString();
    }


    /**
     *      * 生成固定长度随机码
     *      * @param n    长度
     *      
     */
    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
        return rangeLong;
    }

    /**
     *      * 生成不带类别标头的编码
     *      * @param userId
     *      
     */
    private static synchronized String getCode() {
        return /*getDateTime() +*/ toCode(IdWorker.getId());
    }

    /**
     *      * 生成订单单号编码
     *      * @param userId
     *      
     */
    public static String getOrderCode(Long userId) {
        return ORDER_CODE + getCode()+ getLongSoor(userId);
    }


    /**
     *      * 生成流水单号编码
     *      * @param userId
     *      
     */
    public static String getWaterCode(Long userId) {
        return WATER_CODE + getCode()+ getLongSoor(userId);
    }

    /**
     *      * 生成退货单号编码
     *      * @param userId
     *      
     */
    public static String getReturnCode(Long userId) {
        return RETURN_ORDER + getCode()+ getLongSoor(userId);
    }

    /**
     *      * 生成退款单号编码
     *      * @param userId
     *      
     */
    public static String getRefundCode(Long userId) {
        return REFUND_ORDER + getCode()+ getLongSoor(userId);
    }

    /**
     *      * 未付款重新支付
     *      * @param userId
     *      
     */
    public static String getAgainCode(Long userId) {
       String a= AGAIN_ORDER + getCode()+ getLongSoor(userId);
        return a;
    }

    /**
     *  生成二维码编号
     * @param userId
     * @return
     */
    public static String getQRCode(Long userId) {
        return QR_CODE + getCode() + getLongSoor(userId);
    }

    public static void main(String[] args) {
        //System.out.println("452914905844657500696".length());
        int a = 100;
        for (int i = 0; i < a; i++) {
            System.out.println(getAgainCode(1229145844657500696L));
        }

    }


}
